<template>
  <el-form
    :inline="true"
    :model="ruleForm"
    ref="ruleForm"
    :rules="rules"
    label-width="120px"
    size="medium"
  >
    <el-row class="text-center">
      <el-col :span="24">
        <el-form-item label="密码" prop="oldPsd">
          <el-input type="password" v-model="ruleForm.oldPsd" placeholder="请输入密码"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="新邮箱" prop="newPsd">
          <el-input type="text" v-model="ruleForm.newPsd" placeholder="请输入新邮箱"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="确认新邮箱" prop="checkNewPsd">
          <el-input type="text" v-model="ruleForm.checkNewPsd" placeholder="请再次输入新邮箱"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row class="text-center">
      <el-col :span="24">
        <el-form-item size="small" class="text-center subBtn">
          <el-button type="primary" @click="CheckNewPsd">确定修改</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
export default {
  // components: {
  // },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入邮箱"));
      } else {
        if (this.ruleForm.checkNewPsd !== "") {
          this.$refs.ruleForm.validateField("checkNewPsd");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入邮箱"));
      } else if (value !== this.ruleForm.newPsd) {
        callback(new Error("两次输入邮箱不一致!"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      ruleForm: {},
      userData: {},
        formInline: {
        u_id:'',
        password:'',
        username: "",
        realname: "",
        email:''
      },
      rules: {
        oldPsd: [{ required: true, message: "请输入密码", trigger: "blur" }],
        newPsd: [{ validator: validatePass, trigger: "blur" }],
        checkNewPsd: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  methods:{
     CheckNewPsd(){
      this.formInline.u_id = this.$store.getters.getUID;
      this.formInline.username = this.$store.getters.getUser;
      this.formInline.password = this.ruleForm.oldPsd;
      this.formInline.realname = this.$store.getters.getRealName;
      this.formInline.email = this.ruleForm.newPsd
      let data = JSON.stringify(this.formInline);
      console.log(data);
      this.$axios.post("/ssms/common/updateEmail", data,{headers:{'Content-Type':'application/json',
      responseType: "application/json"}})
      .then(res => {
        console.log(res.data);
        if(res.data.code === 0){
          alert("修改成功");
           this.$store.commit("setEmail", this.formInline.email);
            this.$router.push("/userView/myself");
        }
      })
    }
  }
};
</script>
